package com.ihighest.app.infox.activity;

import com.ihighest.app.common.api.Api;
import com.ihighest.app.common.api.ApiCallback;
import com.ihighest.app.common.api.ApiParam;
import com.ihighest.app.common.api.ApiResult;
import com.ihighest.app.common.kit.SharedPreferencesKit;
import com.ihighest.app.common.kit.ToastKit;
import com.ihighest.app.infox.Const;
import com.ihighest.app.infox.R;
import com.ihighest.app.infox.myview.ActivityTitle;

import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.EditText;

/**
 * 填写好友邀请码界面
 * 
 * @author lkx Created at 2015-11-23 下午5:45:07
 * @version 1.0
 */
public class SetFriendInvitedCode extends BaseActivity {
	private ActivityTitle title;
	private EditText code;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.infox_activity_setfriendinvitedcode);
		initView();
	}

	private void initView() {
		title = (ActivityTitle) findViewById(R.id.setFriendInvitedCodeTitle);
		title.setTitle("填写好友邀请码");
		title.setViewVersible(title.titleBack, title.rightText);
		title.setViewGone(title.titleMsg, title.titleRefresh);
		title.setRightTextViewText("完成");
		code = (EditText) findViewById(R.id.friendCode);
		title.rightText.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View arg0) {
				// 完成提交
				String friendCode = code.getText().toString();
				if (TextUtils.isEmpty(friendCode)) {
					ToastKit.showShort(SetFriendInvitedCode.this, "邀请码不能为空");
				} else {
					if (friendCode.equals(SharedPreferencesKit.getString(SetFriendInvitedCode.this, Const.USER_InvitedCode))) {
						ToastKit.showShort(SetFriendInvitedCode.this, "不能填写自己的邀请码");
					} else {
						title.rightText.setClickable(false);
						commitData(friendCode);
					}

				}

			}
		});

	}

	/**
	 * 提交数据
	 * 
	 * @param nickName
	 * 
	 * @author lkx Created at 2015-11-23 下午6:39:18
	 */
	protected void commitData(String nickName) {

		Api.post("me/addCode", ApiParam.create().addParam("invitationCode", nickName), new ApiCallback(SetFriendInvitedCode.this) {

			@Override
			public void handleSuccess(ApiResult result) {

				if (result.getCode() == 100) {
					finish();
				} else {
					title.rightText.setClickable(true);
					if (!TextUtils.isEmpty(result.getMsg())) {
						ToastKit.showShort(SetFriendInvitedCode.this, result.getMsg());
					}

				}
			}

			@Override
			public void handleFailure(Exception e, String msg) {
				title.rightText.setClickable(true);
			}
		}, SetFriendInvitedCode.this);

	}
}
